package com.service.impl;

import com.constant.Constant;
import com.dao.ResourceDao;
import com.dao.UserGpDao;
import com.dao.UserRescourceDao;
import com.entity.GoldPoints;
import com.entity.Resource;
import com.entity.User;
import com.entity.UserRescource;
import com.exception.ServiceException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.ResourceService;
import com.util.AjaxResult;
import com.vo.GoldPointsVo;
import com.vo.ResourceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * date:2021/4/29
 * description:
 * Author:Stargazer
 */
@Service
public class ResourceServiceImpl implements ResourceService {
    @Autowired
    private ResourceDao resourceDao;
    @Autowired
    private UserGpDao userGpDao;
    @Autowired
    private UserRescourceDao userRescourceDao;

    @Override

    public AjaxResult checkResourceByIdAndUser(Integer resourceId, User user) {
        //通过资源id获取从表中查出对应的资源对象
        Resource resource = resourceDao.selectResourceByID(resourceId);
        Integer costType = resource.getCost_type();//资源的支付方式0是积分,1是金币
        Integer costNumber = resource.getCost_number();//该资源需要的金币或积分
        Integer authorId = resource.getUser_id();//资源中的上传用户的id即作者id
        //用户的id
        Integer userId = user.getId();
        List<Integer> integers = userRescourceDao.selectResourceIdsByUserId(userId);
        for (Integer integer : integers) {
            if(integer==resourceId){
                return new AjaxResult(true, "您已购买本课程", resource);
            }

        }
        //根据用户id查询对相应的金币积分对象
        GoldPointsVo goldPointsVoByUserId = userGpDao.getGoldPointsVoByUserId(userId);
        Integer userSumGoldCount = goldPointsVoByUserId.getSum_gold_count();//获取对应的用户金币总量
        Integer userSumPointCount = goldPointsVoByUserId.getSum_point_count();//获取对应的用户积分总量

        //根据作者id查询对相应的金币积分对象
        GoldPointsVo goldPointsVoByAuthorId = userGpDao.getGoldPointsVoByUserId(authorId);
        Integer authorSumGoldCount = goldPointsVoByAuthorId.getSum_gold_count();//获取对应的作者金币总量
        Integer authorSumPointCount = goldPointsVoByAuthorId.getSum_point_count();//获取对应的作者积分总量
        /**
         * 先判断支付方式costType,再判断该资源需要的积分costNumber是否小于用户的积分或者金币总量
         * 由于前段需要获取data.obj.cost_type,和data.obj.file_type所以我觉得AjaxResult里面应该装对应的资源对象,之后再修改数据库的数据
         */
        AjaxResult ajaxResult = null;
        if (costType == 0) {//积分,
            if (costNumber <= userSumPointCount) {
                //用户的goldPoints,用来修改用户积分的
                GoldPoints userPoints = new GoldPoints();
                userPoints.setUser_id(user.getId());
                userPoints.setPoint_count(-costNumber);
                userPoints.setInfo("视频花费了" + costNumber + "点积分");
                userPoints.setCreate_date(new Date());
                //用户的AuthorPoints,用来修改用户积分的
                GoldPoints authorPoints = new GoldPoints();
                authorPoints.setUser_id(resource.getUser_id());
                authorPoints.setPoint_count(costNumber);
                authorPoints.setInfo("点击视频增加了" + costNumber + "点积分");
                authorPoints.setCreate_date(new Date());
                //修改积分表
                userGpDao.addPointOrGold(userPoints);//给用户减少积分
                userGpDao.addPointOrGold(authorPoints);//给作者增加积分
                userRescourceDao.insertUserResource(userId, resourceId);//在uer资源表中插入资源
                ajaxResult = new AjaxResult(true, "扣除了" + costNumber + "点积分", resource);
            } else {
                System.out.println("积分不足");
                ajaxResult.setSuccess(false);
                ajaxResult.setMsg("积分不足,积极参与活动获取更多积分吧！");
            }
        } else if (costType == 1) {//用金币的情况
            if (costNumber <= userSumGoldCount) {
                //用户的goldPoints,用来修改用户金币
                GoldPoints uGold = new GoldPoints();
                uGold.setUser_id(user.getId());
                uGold.setGold_count(-costNumber);
                uGold.setInfo("视频花费了" + costNumber + "点金币");
                uGold.setCreate_date(new Date());
                //用户的AuthorPoints,用来修改用户积分的
                GoldPoints aGold = new GoldPoints();
                aGold.setUser_id(resource.getUser_id());
                aGold.setGold_count(costNumber);
                aGold.setInfo("点击视频增加了" + costNumber + "点金币");
                aGold.setCreate_date(new Date());
                //修改积分表
                userGpDao.addPointOrGold(uGold);
                userGpDao.addPointOrGold(aGold);
                userRescourceDao.insertUserResource(userId, resourceId);//在uer资源表中插入资源
                ajaxResult = new AjaxResult(true, "扣除了" + costNumber + "点金币", resource);
            } else {
                System.out.println("金币不足");
                ajaxResult.setSuccess(false);
                ajaxResult.setMsg("金币不足,积极参与活动获取更多金币吧！");
            }
        }
        return ajaxResult;
    }

    @Override
    public Resource findResourceByResourceId(Integer resourceId) {
        Resource resource = resourceDao.selectResourceByID(resourceId);
        return resource;
    }

    //luo
//luo
    @Override
    public PageInfo<ResourceVo> findBackResource(Integer page, ResourceVo resourceVo) {
        List<ResourceVo> list = null;
        try {
            PageHelper.startPage(page, Constant.PAGE_SIZE);
            list = resourceDao.findBackResource(resourceVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new PageInfo<ResourceVo>(list);
    }

    @Override
    public void toggleStatus(Resource resource) {
        try {
            resourceDao.toggleStatus(resource);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Resource down(Integer id) {
        return resourceDao.down(id);
    }

    //xu
    @Override
    public Boolean addResource(Resource resource) throws ServiceException {
        int result =resourceDao.insertByResource(resource);
        return result>0?true:false;
    }

    @Override
    public Resource findPathById(int id) throws ServiceException {
        return resourceDao.selectPathById(id);
    }

    @Override
    public int findClickCountById(int id) throws ServiceException {
        return resourceDao.selectClickCountById(id);
    }

    @Override
    public Boolean changeClickCountById(int id, int click_count) throws ServiceException {
        int result =resourceDao.updateClickCountById(id,click_count);
        return result>0?true:false;
    }

    //查询资源标题是否存在
    @Override
    public int existsTitlt(String title) throws ServiceException {
        return resourceDao.selectCountByTitle(title);
    }

    @Override
    public PageInfo<ResourceVo> findBackResource(int pageNo, int userId) {
        PageHelper.startPage(pageNo, Constant.PAGE_SIZE);
        List<ResourceVo> resourceVo=resourceDao.selectResourceByUserId(userId);
        for (int i=0;i<resourceVo.size();i++){
        }
        return new PageInfo<ResourceVo>(resourceVo);
    }

    @Override
    public void modifyFrontResource(Resource resource) {
        int i=resourceDao.updateResource(resource);
        if (i!=1){
            throw new ServiceException("修改数据失败");
        }
    }

    @Override
    public AjaxResult removeFrontResource(int id) {
        int i=resourceDao.deleteResourceById(id);
        if (i!=1){
            return AjaxResult.isOk("数据修改失败");
        }
        return AjaxResult.isOk("成功删除");
    }

    @Override
    public PageInfo findResourceByPageNo(ResourceVo resourceVo, Integer pageNo) {
        PageHelper.startPage(pageNo,Constant.PAGE_SIZE);
        List<ResourceVo> resourceVoList=resourceDao.findResourceByPageNo(resourceVo);
        PageInfo<ResourceVo> info=new PageInfo<>(resourceVoList);
        return info;
    }

    @Override
    public void changeResourceStatusById(int id, int status) {

    }

}
